//导入vue
import Vue from 'vue'
//导入vue-Router
import VueRouter from 'vue-router'

//集成vueRouter中间件
Vue.use(VueRouter)
//导入组件
import Login from '../components/account/Login.vue'
//导入布局组件
import Layout from '../components/Layout/Layout.vue'
import Users from '../components/Users/Users.vue'
import Rights from '../components/rights/Rights.vue'
import Roles from '../components/roles/Roles.vue'
import Categories from '../components/goods/Categories.vue'
import Params from '../components/goods/Params.vue'
import Goods from '../components/goods/Goods.vue'
import GoodsList from '../components/goods/GoodsList.vue'
import Addgoods from  '../components/goods/Addgoods.vue'
import Editgoods from '../components/goods/Editgoods.vue'
import Orders from '../components/orders/Orders.vue'
import Reports from '../components/reports/Reports.vue'
//创建路由对象
const router =new VueRouter({
    routes:[
        {path:'/',redirect:'login'},
        {path:'/login', meta: { unRequiresAuth: true },component:Login},
        {path:'/layout',component:Layout,children:[
            {path:'users',component:Users},
            {path:'rights',component:Rights},
            {path:'roles',component:Roles},
            {path:'categories',component:Categories},
            {path:'params',component:Params},
            {path:'goods',component:Goods,children:[
                {path:'',component:GoodsList}, 
                {path:'addgoods',component:Addgoods}, 
                {path:'editgoods',component:Editgoods} 
            ]},
            {path:'orders',component:Orders},
            {path:'reports',component:Reports},
        ]},
    ]
})
/**
 * 导航守卫
 * to要去哪个路由
 * from从哪个路由去
 * next 控制是否继续
 */
router.beforeEach((to, from, next) => {
   if(to.meta.unRequiresAuth){//不需要验证
       next()
   }else{//需要权限验证
    //判断本地是否有token,如果有就代表登录
    if(localStorage.getItem('mytoken')){
        next()
    }else{
        return router.push({path:'/login'})
    }
   }
  })
//导出
export default router